/**
 * 
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IPlaceAttestationDao;
import com.qiyou.entity.PlaceAttestation;
import com.qiyou.entity.ScenicSpot;
import com.qiyou.entity.UserInfo;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * @author 赛艳志
 *
 */
public class PlaceAttestationDaoImpl implements IPlaceAttestationDao {
    private Connection conn;
    
    public PlaceAttestationDaoImpl(Connection conn){
        this.conn = conn;
    }
    
    @Override
    public boolean doCreate(PlaceAttestation attestation) throws SQLException {
        String sql = "insert into tb_place_attestation(sceID,userID,attDate) values(?,?,?)";
        Object[] objects = TransformUtil.beanTOArray(attestation, false);
        return DBUtil.execute(conn, sql, objects);
    }

    @Override
    public boolean doDelte(long scenicSpotID, String userID)
            throws SQLException {
        String sql = "delete from tb_place_attestation where sceID=? and userID=?";
        return DBUtil.execute(conn, sql, scenicSpotID,userID);
    }

    @Override
    public List<UserInfo> getPlaceAttestations(long scenicSpotID)
            throws SQLException {
        List<UserInfo> uilist = null;
        String sql = "select tb_user_info.* from tb_user_info,tb_place_attestation " +
        		"where tb_place_attestation.sceID=? and tb_user_info.userID=tb_place_attestation.userID";
        List<Object[]> all = DBUtil.query(conn, sql, scenicSpotID);
        if (null!=all&&all.size()>0) {
            UserInfo userInfo = null;
            uilist = new ArrayList<UserInfo>(all.size());
            for (Object[] objects : all) {
                userInfo = (UserInfo) TransformUtil.arrayToBean(objects, UserInfo.class);
                uilist.add(userInfo);
            }
        }
        return uilist;
    }

    @Override
    public List<ScenicSpot> getPlaceAttestations(String userID)
            throws SQLException {
        List<ScenicSpot> sslist = null;
        String sql = "select tb_scenic_spots.* from tb_scenic_spots,tb_place_attestation " +
        		"where tb_place_attestation.userID=? and tb_scenic_spots.scsID=tb_place_attestation.sceID";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (null!=all&&all.size()>0) {
            sslist = new ArrayList<ScenicSpot>(all.size());
            ScenicSpot scenicSpot = null;
            for (Object[] objects : all) {
                scenicSpot = (ScenicSpot) TransformUtil.arrayToBean(objects, ScenicSpot.class);
                sslist.add(scenicSpot);
            }
        }
        return sslist;
    }

}
